package com.orm.mybatis.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import cn.hutool.core.date.DateTime;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class SSHUtil {
//    String hostname = "192.168.100.50";
//    int port = 22;//22 usually the default port
//    String username = "root";
//    String password = "highgo";
    //指明连接主机的IP地址
    Connection conn = null;
//   = new Connection(hostname,port);
    Session ssh = null;
    SCPClient clt = null;
    Session sess =null;

    public boolean connect(String hostname, int port, String username, String password) throws IOException {
        conn = new Connection(hostname, port);
        conn.connect();
        boolean isconn = conn.authenticateWithPassword(username, password);
        if (isconn) {
            return true;
        }
        log.debug("服务器账号密码错误");
        conn.close();
        return false;
    }


    public void sCPLocalToLinux(String localPathFile,String remotePath) throws IOException {
        clt = conn.createSCPClient();
        //以下是linux的示例
        //将本地conf/server_start.sh传输到远程主机的/opt/pg944/目录下
//        clt.put("conf/server_start.sh", "/home/lwq/");
        clt.put(localPathFile, remotePath);
    }

    public void linuxExecShell(String command) throws IOException {
        ssh = conn.openSession();
        ssh.execCommand(command);
        //ssh.execCommand("perl /root/hello.pl");
        //只允许使用一行命令，即ssh对象只能使用一次execCommand这个方法，多次使用则会出现异常.
        //使用多个命令用分号隔开
        //ssh.execCommand("cd /root; sh hello.sh");
        //将Terminal屏幕上的文字全部打印出来
        InputStream is = new StreamGobbler(ssh.getStdout());
        BufferedReader brs = new BufferedReader(new InputStreamReader(is));
        while (true)
        {
            String line = brs.readLine();
            if (line == null)
            {
                break;
            }
            System.out.println(line);
        }
    }

    public void windosExecShell() {
    /* 执行windows系统命令的示例
                sess = conn.openSession();
                sess.execCommand("ipconfig");

                */
    }


    /**
     * 断开与远程服务器的连接
     * @throws IOException
     *
     */
    public void disconnect(){
        if(ssh!=null)
        {
            ssh.close();
        }
        if(conn!=null)
        {
            conn.close();
        }
        if (sess!=null){
            sess.close();
        }
        log.debug("正常关闭");
    }

    public static void main(String[] args) {
//        SSHUtil sshUtil = new SSHUtil();
//        try {
//            sshUtil.connect("88.88.88.142",22,"lwq","123456");
//            sshUtil.linuxExecShell("cd /home/lwq/临时存储区/临时存储区;touch test.txt");
//            sshUtil.disconnect();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
//        //ca.setTime(new Date()); // 设置时间为当前时间
//        ca.set(2011, 11, 17);// 月份是从0开始的，所以11表示12月
//        ca.add(Calendar.YEAR, -1); // 年份减1
//        ca.add(Calendar.MONTH, -1);// 月份减1
//        ca.add(Calendar.DATE, -1);// 日期减1
//        Date resultDate = ca.getTime(); // 结果

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateTime.now());
        calendar.add(Calendar.DATE,-1);
        Date date = calendar.getTime();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
//        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
    }

}
